package com.cjy.userReport.service.repair.impl;

import com.base.common.ConfigConstants;
import com.base.common.entity.RepairTicket;
import com.base.common.enums.TicketCategories;
import com.base.common.persistent.dao.TicketMapper;
import com.base.common.persistent.model.Ticket;
import com.base.common.utils.EasyUtil;
import com.cjy.common.bean.Files;
import com.cjy.userReport.service.base.TicketBaseServiceImpl;
import com.cjy.userReport.service.repair.RepairService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("repairService")
public class RepairServiceImpl extends TicketBaseServiceImpl implements RepairService {
	@Resource
	private EasyUtil easyUtil;
	
	@Resource
	private TicketMapper ticketMapper;

	@Override
	public List<RepairTicket> getRepairMessagesByUser(int userId, int page) {
		List<RepairTicket> repairTickets = new ArrayList<RepairTicket>();
		List<Ticket> tickets = ticketMapper.queryLastByCategoriesAndUserId(getStartNum(page), Integer.parseInt(ConfigConstants.INFOS_LIST_NUM), TicketCategories.REPAIR.getValue(), userId);
		for(Ticket ticket : tickets){
			System.out.println(ticket.getBespeakTime());
			RepairTicket repairTicket = new RepairTicket(ticket.getId(), ticket.getCode(), ticket.getAddress(), ticket.getContext(), ticket.getCategories(), ticket.getGetTime(), ticket.getFinishTime(), ticket.getBespeakTime(), ticket.getStatus(), ticket.getPicUrls(), ticket.getRemarks(), ticket.getUser().getId(), ticket.getTicketScore());
			repairTickets.add(repairTicket);
		}
		return repairTickets;
	}
	

	/**
	 * 生成临时工单工单号
	 * 
	 * @param tickets
	 * @return
	 */
	public String getTemporaryCode(List<Ticket> tickets) {
		try {
			tickets = ticketMapper.queryLastCodeByCategories(TicketCategories.REPAIR.getValue());
		} catch (Exception e) {
			e.printStackTrace();
		}
		String code = "";
		DecimalFormat df = new DecimalFormat("000000");
		if(tickets.size() != 0){
			String lastedCode = tickets.get(0).getCode();
			code = df.format(Integer.parseInt(lastedCode.substring(1).trim()) + 1);
		} else {
			code = df.format(1);
		}
		code = TicketCategories.REPAIR.getName() + code;
		return code;
	}


	@Transactional
	@Override
	public String subRepairMessage(Ticket ticket, Files files, String basePath, int bid) {
		// 设置接单时间
		ticket.setGetTime(easyUtil.getDateStrByGeneral(new Date()));
		
		// 设置状态为派单
//		ticket.setStatus(0);
		
		// 设置工单为临时工单
		ticket.setCategories(TicketCategories.REPAIR.getValue());
		
		// 设置工单号，查询最新临时工单号，并在基础上加1
		List<Ticket> tickets = new ArrayList<Ticket>();
		String code = getTemporaryCode(tickets);
		ticket.setCode(code);
		
		// 图片相对保存地址
		String sysImagesFile = "/resources/images/tickets" + "/"  + code;
		// 保存图片
		String[] fileNames = files.getFileFileName();
		String picUrls = "";
		try {
			if(bid == 2) {
				picUrls = easyUtil.downloadFile(ticket.getPicUrls(), basePath, sysImagesFile, picUrls);
			} else {
				if(fileNames != null && fileNames.length != 0){
					picUrls = easyUtil.savePictures(files.getFile(), basePath, sysImagesFile,
							fileNames, picUrls);
				}
			}	
		} catch (IOException e) {
			return null;
		}
		
		// 添加图片地址
		ticket.setPicUrls(picUrls);
		int result = ticketMapper.addTicket(ticket);
		if(result > 0){
			return code;
		}
		return null;
	}

	public String test(){
		System.out.println("111");
		return "abc";
	}
	
}
